package com.inno.subject.controller.controller;

import com.google.common.base.Preconditions;
import com.inno.subject.common.annotiation.Log;
import com.inno.subject.common.context.LoginUserContext;
import com.inno.subject.common.entity.CommonReturnType;
import com.inno.subject.common.entity.PageResult;
import com.inno.subject.controller.convert.SubjectRecordsDTOConverter;
import com.inno.subject.controller.convert.SubjectSolveDTOConverter;
import com.inno.subject.controller.dto.SubjectRecordsDTO;
import com.inno.subject.controller.dto.SubjectSolveDTO;
import com.inno.subject.domain.entity.SubjectRecordsBO;
import com.inno.subject.domain.entity.SubjectSolveBO;
import com.inno.subject.domain.service.SolveSubjectDomainService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author sean
 * 答题controller
 */
@RestController
@RequestMapping("/subject/solve")
public class SolveSubjectController {
    @Resource
    private SolveSubjectDomainService solveSubjectDomainService;

    /**
     * 提交
     */
    @PostMapping("/confirm")
    @Log(operationType = "答题-提交答案")
    public CommonReturnType<SubjectSolveDTO> confirm(@RequestBody SubjectSolveDTO subjectSolveDTO){
        Preconditions.checkNotNull(subjectSolveDTO.getSubjectId(), "题目id不能为空");
        Preconditions.checkArgument(!StringUtils.isBlank(subjectSolveDTO.getSubjectAnswer()),"题目答案不能为空");
        subjectSolveDTO.setCheckDone(subjectSolveDTO.getCheckDone() != null && subjectSolveDTO.getCheckDone());
        subjectSolveDTO.setCurrentUser(LoginUserContext.getUserId());
        SubjectSolveBO subjectSolveBO = SubjectSolveDTOConverter.INSTANCE.convertDTOToBO(subjectSolveDTO);
        SubjectSolveBO bo = this.solveSubjectDomainService.confirm(subjectSolveBO);
        SubjectSolveDTO dto = SubjectSolveDTOConverter.INSTANCE.convertBOToDTO(bo);
        return CommonReturnType.success(dto);
    }

    /**
     * 答题记录
     */
    @PostMapping("/queryRecord")
    public CommonReturnType<PageResult<SubjectRecordsDTO>> solveRecord(@RequestBody SubjectRecordsDTO subjectRecordsDTO){
        Preconditions.checkNotNull(subjectRecordsDTO.getPage(), "分页参数不能为空");
        Preconditions.checkNotNull(subjectRecordsDTO.getPageSize(), "分页参数不能为空");
        subjectRecordsDTO.setCurrentUserId(LoginUserContext.getUserId());
        SubjectRecordsBO subjectRecordsBO = SubjectRecordsDTOConverter.INSTANCE.convertDTOToBO(subjectRecordsDTO);
        PageResult<SubjectRecordsBO> boResult =  this.solveSubjectDomainService.queryRecord(subjectRecordsBO);
        List<SubjectRecordsDTO> dtoList = SubjectRecordsDTOConverter.INSTANCE.convertBOListToDTOList(boResult.getList());
        return CommonReturnType.success(PageResult.of(dtoList,boResult.getTotal()));
    }
}
